package com.flyco.modules.sync.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;

import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.log.service.ILogSyncSkuPriceService;
import com.flyco.modules.oms.handle.IOmsOrderHandle;
import com.flyco.modules.oms.handle.IOmsOrderReturnApplyHandle;
import com.flyco.modules.oms.handle.ISyncLogisticsHandle;
import com.flyco.modules.oms.service.IOmsOrderTraceService;
import com.flyco.modules.pms.service.IPmsCommentService;
import com.flyco.modules.system.entity.SysPartner;
import com.flyco.modules.system.service.ISysPartnerService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @Description: 同步
 * @Author: flyco
 * @Date:   2020-03-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags="同步")
@RestController
@RequestMapping("/sync")
public class SyncController {
	@Autowired
	private ILogSyncSkuPriceService logSyncSkuPriceService;
	@Autowired
	private IOmsOrderTraceService omsOrderTraceService;
	@Autowired
	private IOmsOrderHandle omsOrderHandle;
	@Autowired
	private IOmsOrderReturnApplyHandle omsOrderReturnApplyHandle;
	@Autowired
	private ISyncLogisticsHandle syncLogisticsHandle;
	@Autowired
	private IPmsCommentService pmsCommentService;
	@Autowired
	private ISysPartnerService sysPartnerService;

	 /**
	  * 同步商品限价
	  *
	  * @return
	  */
	 @ApiOperation(value="商品价格同步-同步", notes="商品价格同步-同步")
	 @GetMapping(value = "/syncSkuPrice")
	 public Result<?> syncSkuPrice() {
		 LambdaQueryWrapper<SysPartner> queryWrapper = new LambdaQueryWrapper();
		 queryWrapper.isNotNull(SysPartner::getExtCode);
		 queryWrapper.eq(SysPartner::getDelFlag, 0);
		 List<SysPartner> list = sysPartnerService.list(queryWrapper);
		 for ( SysPartner partner :  list) {
			 logSyncSkuPriceService.syncSkuPrice2(partner);
		 }
		 return Result.ok("同步成功！");
	 }

	 /**
	  * 同步物流轨迹
	  *
	  * @return
	  */
	 @ApiOperation(value="物流轨迹-同步", notes="物流轨迹-同步")
	 @GetMapping(value = "/syncLogistics")
	 public Result<?> syncLogistics() {
		 omsOrderTraceService.syncLogistics();
		 return Result.ok("同步成功！");
	 }

	 /**
	  * 同步带快递单号的物流轨迹
	  *
	  * @return
	  */
	 @ApiOperation(value="快递单号物流轨迹-同步", notes="快递单号物流轨迹-同步")
	 @GetMapping(value = "/syncLogisticsByCode")
	 public Result<?> syncLogisticsByCode(@RequestParam(name="code",required=true) String code) {
		 //syncLogisticsHandle.syncLogistics(code, null);
		 return Result.ok("同步成功！");
	 }

	 /**
	  * 获取wing系统中的订单状态
	  * Wing系统状态
	  * 	1.Wing 2.0里面的状态有: 1. 未确认,2. 缺货,3. 已审核,4. 配货中,5. 仓库发货,6. 平台发货,7. 已取消,8.系统作废,9. 预售,10 代发,11. 物流已送达,12. 交易完成
	  * 	其中1,2,3,4 为未发货状态
	  * 	5,6 表示系统已经发货,已经状态已经不可逆
	  * 	7,8 表示单据已经取消,只有未发货的状态才能取消或作废订单
	  * 	9,10 为未知状态,一般系统很少出现这两种状态
	  * 	11,12 状态为其他系统告知的业务状态,除了会员关怀外,其他还未有其他使用场景
	  * 注意事项
	  * 	如果返回OrderId为0,OrderStatus为0,则表明订单不存在
	  * 	如果返回OrderId为0,OrderStatus为7或则8,则表明订单被取消或作废
	  * 	如果OrderId不为0,则认为订单为正常状态
	  *
	  *
	  * @return
	  */
	 @ApiOperation(value="获取wing系统中的订单状态", notes="获取wing系统中的订单状态")
	 @GetMapping(value = "/getOmsOrderStatusByOrderId")
	 public Result<?> getOmsOrderStatusById(@RequestParam(name="orderId",required=true) String orderId) {
		 String status = omsOrderHandle.getOmsOrderStatus(orderId);
		 return Result.ok(status);
	 }


	 /**
	  * 用户退款
	  *
	  * @return
	  */
	 @ApiOperation(value="快用户退款-同步", notes="用户退款-同步")
	 @GetMapping(value = "/syncRefundApplyById/{orderId}/{applyId}")
	 public Result<?> syncRefundApplyById(@PathVariable(name="orderId",required=true) String orderId,@PathVariable(name="applyId",required=true) String applyId) {
		 String result = omsOrderReturnApplyHandle.sendOrderRefundById(orderId, applyId);
		 if (! oConvertUtils.isEmpty(result)) {
			 JSONObject resultObj = JSON.parseObject(result);
			 return Result.ok(resultObj.getString("message"));
		 }
		 return Result.error("调用没有返回记录");
	 }


	 /**
	  * 用户退款
	  *
	  * @return
	  */
	 @ApiOperation(value="快用户退款-同步", notes="用户退款-同步")
	 @GetMapping(value = "/syncRefundApply")
	 public Result<?> syncRefundApply() {
		 omsOrderReturnApplyHandle.sendOrderRefund();
		 return Result.ok("同步成功！");
	 }


	 /**
	  * 同步评价
	  *
	  * @return
	  */
	 @ApiOperation(value="同步评价", notes="同步评价")
	 @GetMapping(value = "/syncComment")
	 public Result<?> syncComment() {
		 pmsCommentService.syncFromBosTmComment();
		 return Result.ok("同步成功！");
	 }

}
